using System;
using System.IO;
namespace IFramework
{
    public abstract class DownLoader : IDownLoader
    {
        protected Action<float> CallProgress;
        protected float progress;
        protected string url;
        protected string savePath;
        protected string fileNameWithoutExt;
        protected string fileExt;
        protected string saveFilePath;
        protected long fileLength;
        protected long currentLength;
        protected bool isStartDownload;
        public virtual float Progress { get { return progress; } }
        public string Url { get { return url; } }
        public string SavePath { get { return savePath; } }
        public string FileNameWithoutExt { get { return fileNameWithoutExt; } }
        public string FileExt { get { return fileExt; } }
        public string SaveFilePath { get { return saveFilePath; } }
        public virtual long FileLength { get { return fileLength; } }
        public virtual long CurrentLength { get { return currentLength; } }
        public bool IsStartDownload { get { return isStartDownload; } }
        
        public DownLoader(string url, string savePath, Action<float> CallProgress)
        {
            this.CallProgress = CallProgress;
            this.url = url;
            this.savePath = savePath;
            isStartDownload = false;
            fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(url);
            fileNameWithoutExt = fileNameWithoutExt.Replace(".", "").Replace("=", "").Replace("%", "").Replace("&", "").Replace("?", "");
            fileExt = System.IO.Path.GetExtension(url);
            saveFilePath = string.Format("{0}/{1}{2}", savePath, fileNameWithoutExt, fileExt);
        }
        public virtual void DownLoad()
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(savePath)) return;
            if (!string.IsNullOrEmpty(saveFilePath))
            {
                string dirName = Path.GetDirectoryName(saveFilePath);
                if (!Directory.Exists(dirName))
                {
                    Directory.CreateDirectory(dirName);
                }
            }
        }

        public virtual void Destroy()
        {
        }
    }
}